﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;

namespace Marmat.Data
{
    public partial class SQLClassHelper
    {
        public static string GetSqlString(object Source, SQLBaseHelper.ModeEnum State)
        {
            var Helper = new SQLBaseHelper();
            var Sql = new System.Text.StringBuilder();
            var Type = Source.GetType();
            var TableInfos = Type.GetCustomAttributes(typeof(Table), false);
            if (TableInfos.Length > 0)
            {
                Table TableInfo = (Table)TableInfos[0];
                Helper.TableName = TableInfo.TableName;
                var Properties = Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                foreach (PropertyInfo Info in Properties)
                {
                    var PrimaryKeys = Info.GetCustomAttributes(typeof(PrimaryKey), false);
                    var Columns = Info.GetCustomAttributes(typeof(Column), false);
                    var NotNulls = Info.GetCustomAttributes(typeof(NotNull), false);
                    if (Columns.Length > 0)
                    {
                        var Value = Info.GetValue(Source, null);
                        if (NotNulls.Length > 0 && (Value is null || Value == DBNull.Value))
                        {
                            throw new Exception("属性 " + Info.Name + " 声明了 NotNull,所以值不能为空。");
                        }

                        Column Column = (Column)Columns[0];
                        var Item = new SQLBaseHelper.Column();
                        Item.ColumnName = Column.ColumnName;
                        if (string.IsNullOrEmpty(Column.ColumnName))
                        {
                            Item.ColumnName = Info.Name;
                        }

                        Item.DataType = Column.DataType;
                        if (Column.DataType is null)
                        {
                            Item.DataType = Info.PropertyType;
                        }

                        Item.MaxLength = Column.MaxLength;
                        Item.Value = Value;
                        if (PrimaryKeys.Length > 0)
                        {
                            Helper.PrimaryKey.Add(Item);
                        }
                        else
                        {
                            Helper.Columns.Add(Item);
                        }
                    }
                }
            }

            return Helper.GetSqlString(State);
        }

        public static void Get(IList Source, Type Type, DataTable DataTable)
        {
            foreach (DataRow dr in DataTable.Rows)
            {
                var Item = Type.Assembly.CreateInstance(Type.FullName);
                Get(Item, dr);
                Source.Add(Item);
            }
        }

        public static void Get(object Source, DataRow DataRow)
        {
            var Type = Source.GetType();
            var Properties = Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo Info in Properties)
            {
                var Columns = Info.GetCustomAttributes(typeof(Column), false);
                if (Columns.Length > 0 && Info.CanWrite == true)
                {
                    Column Column = (Column)Columns[0];
                    string ColumnName = Info.Name;
                    if (!string.IsNullOrEmpty(Column.ColumnName))
                    {
                        ColumnName = Column.ColumnName;
                    }
                    Info.SetValue(Source, DataRow[ColumnName], null);
                }
            }
        }
    }
}